package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.Enterprise;
import com.ruoyi.business.domain.IntegralSetting;
import com.ruoyi.business.mapper.EnterpriseMapper;
import com.ruoyi.business.mapper.IntegralSettingMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.IntegralDetailMapper;
import com.ruoyi.business.domain.IntegralDetail;
import com.ruoyi.business.service.IIntegralDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分明细Service业务层处理
 * 
 * @author lcz
 * @date 2025-01-19
 */
@Service
public class IntegralDetailServiceImpl implements IIntegralDetailService 
{
    @Autowired
    private IntegralDetailMapper integralDetailMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IntegralSettingMapper integralSettingMapper;

    /**
     * 查询积分明细
     * 
     * @param id 积分明细主键
     * @return 积分明细
     */
    @Override
    public IntegralDetail selectIntegralDetailById(Long id)
    {
        return integralDetailMapper.selectIntegralDetailById(id);
    }

    /**
     * 查询积分明细列表
     * 
     * @param integralDetail 积分明细
     * @return 积分明细
     */
    @Override
    public List<IntegralDetail> selectIntegralDetailList(IntegralDetail integralDetail)
    {
        return integralDetailMapper.selectIntegralDetailList(integralDetail);
    }

    /**
     * 新增积分明细
     * 
     * @param integralDetail 积分明细
     * @return 结果
     */
    @Override
    public int insertIntegralDetail(IntegralDetail integralDetail)
    {
        integralDetail.setCreateTime(DateUtils.getNowDate());
        return integralDetailMapper.insertIntegralDetail(integralDetail);
    }

    /**
     * 修改积分明细
     * 
     * @param integralDetail 积分明细
     * @return 结果
     */
    @Override
    public int updateIntegralDetail(IntegralDetail integralDetail)
    {
        integralDetail.setUpdateTime(DateUtils.getNowDate());
        return integralDetailMapper.updateIntegralDetail(integralDetail);
    }

    /**
     * 批量删除积分明细
     * 
     * @param ids 需要删除的积分明细主键
     * @return 结果
     */
    @Override
    public int deleteIntegralDetailByIds(Long[] ids)
    {
        return integralDetailMapper.deleteIntegralDetailByIds(ids);
    }

    /**
     * 删除积分明细信息
     * 
     * @param id 积分明细主键
     * @return 结果
     */
    @Override
    public int deleteIntegralDetailById(Long id)
    {
        return integralDetailMapper.deleteIntegralDetailById(id);
    }

    @Transactional
    @Override
    public AjaxResult frontBlessingBag() {
        Long id = tokenService.getUserId();
        Enterprise enterprise = enterpriseMapper.selectEnterpriseById(id);
        if (enterprise.getIntegral()<1l){
            return AjaxResult.error("企业积分不足，无法进行福袋抽奖！");
        }

        IntegralSetting integralSetting = integralSettingMapper.selectIntegralSettingById(2l);

        enterprise.setIntegral(enterprise.getIntegral()-integralSetting.getIntegralPoints());
        enterpriseMapper.updateEnterprise(enterprise);

        IntegralDetail integralDetail = new IntegralDetail();
        integralDetail.setEnterpriseId(id);
        integralDetail.setIntegral(-integralSetting.getIntegralPoints());
        integralDetail.setSource("福袋抽奖扣除积分");
        integralDetail.setCreateTime(new Date());
        integralDetailMapper.insertIntegralDetail(integralDetail);
        return AjaxResult.success();
    }
}
